package com.open.capacity.uaa.server.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.google.code.kaptcha.Producer;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.uaa.server.service.ValidateCodeService;

/**
 * 验证码提供
 *
 * @author zlt
 * @date 2018/12/18
 */
@Controller
public class ValidateCodeController {
    @Autowired
    private Producer producer;

    @Autowired
    private ValidateCodeService validateCodeService;

    /**
     * 创建验证码
     *
     * @throws Exception
     */
    @GetMapping("/validata/code/{deviceId}")
    @LogAnnotation(module = "auth-server", recordRequestParam = false)
    public void createCode(@PathVariable String deviceId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
//        原代码太丑
//        response.setContentType("image/jpeg");
//        //生成文字验证码
//        String text = producer.createText();
//        //生成图片验证码
//        BufferedImage image = producer.createImage(text);
//        validateCodeService.saveImageCode(deviceId, text);
//        try (
//                ServletOutputStream out = response.getOutputStream()
//                ) {
//            ImageIO.write(image, "JPEG", out);
//        }


        //创建空白图片
        // Start update for Jxingyu  2021年5月13日
        BufferedImage image = new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        Random r = new Random();//随机数
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        // 绘制矩形的背景
        g.fillRect(0, 0, 100, 30);
        // 生成文字验证码
        String number = getNumber(3);
        validateCodeService.saveImageCode(deviceId, number);
        System.out.println("Captcha:" + number);
        req.getSession().setAttribute("code", number);
        g.setColor(new Color(0, 0, 0));
        g.setFont(new Font("Mono", Font.BOLD, 36));
        //5.设置颜色字体后，绘制字符串
        g.drawString(number, 25, 25);
        //6.绘制10条干扰线
        for (int i = 0; i < 10; i++) {
            g.setColor(new
                    Color(r.nextInt(156), r.nextInt(166), r.nextInt(156), r.nextInt(255)));
            g.drawLine(r.nextInt(100),
                    r.nextInt(30),
                    r.nextInt(100),
                    r.nextInt(30));
            // 直线参数 （a,b,x,y),a,b代表起点，x，y代表终点
        }
        resp.setHeader("Cache-Control", "no-store, no-cache");
        resp.setContentType("image/jpeg");
        OutputStream ops = resp.getOutputStream();
        ImageIO.write(image, "JPEG", ops);
        ops.close();
    }


    /**
     * 字符串的拼接
     *
     * @param size
     * @return
     */
    private String getNumber(int size) {
        String str = "qwertyuopasdfghjkzxcvbnm";
        StringBuilder number = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < size; i++) {
            number.append(str.charAt(r.nextInt(str.length())));
        }
        return number.toString();
    }
    // End update for Jxingyu  2021年5月13日

}
